package controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.LogEnum;
import beans.RunningTask;
import dao.HistoryTaskDao;
import engine.BIEngine0_1.TaskThread;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import utils.UserLogUtil;

/**
 * Servlet implementation class PauseTask
 */
public class PauseTask extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PauseTask() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取userId
		String userId = (String) request.getSession().getAttribute("userId");
		String taskHash = request.getParameter("taskHash");
		List<Object> taskHashes = new HistoryTaskDao().getSubTaskHashByBatchTaskHash(taskHash).getDatas();
		JSONArray resultArray = new JSONArray();
		if(taskHashes.size()<1){
			response.getWriter().write("error");
		}else{
			//2018-7-21修改为只返回true error finished，下面的是修改之前的内容
//			for(Object one:taskHashes){
//				TaskThread task = RunningTask.TASKMAP.getTask((String) one);
//				JSONObject oneResult = new JSONObject();
//				if (task == null) {
//					oneResult.put(one,"error");
//				} else {
//					if(task.runningTaskBean.runningAlgorithms.size()>0){
//						boolean result = task.pauseTask();
//						if(result)
//						{
//							oneResult.put(one,"true");
//						}
//						else
//						{
//							oneResult.put(one,"error");
//						}
//					}else{
//						oneResult.put(one, "finished");
//					}
//				}
//				resultArray.add(oneResult);
//			}
//			response.getWriter().write(resultArray.toString());
//			// 日志记录
//			UserLogUtil.dbLog(LogEnum.historyTask, LogEnum.pauseHistoryTask,
//					userId, LogEnum.historyTask.getValue()+"哈希值："+taskHash);
			//下面是修改之后的内容
			boolean isPauseSuccess = true;
			boolean isFinished = true;
			boolean isError = false;
			for(Object one:taskHashes){
				TaskThread task = RunningTask.TASKMAP.getTask((String) one);
				if (task == null) {
					//如果子任务中出现了任务池中不存在的任务就报错
					isError = true;
					break;
				} else {
					//如果有子任务还未完成的话，就去pause，pause成功的话，返回true;pause失败的话，返回error
					if(task.runningTaskBean.runningAlgorithms.size()>0){
						isPauseSuccess = isPauseSuccess&&task.pauseTask();
						isFinished = false;
					}
					//将内存中的TaskThread对象移除
					RunningTask.TASKMAP.removeTask((String)one);
				}
			}
			if((!isPauseSuccess)||isError)
			{
				response.getWriter().write("error");
			}else
			{
				if(isFinished)
				{
					response.getWriter().write("finished");
				}else
				{
					response.getWriter().write("true");
				}
			}


			// 日志记录
			UserLogUtil.dbLog(LogEnum.historyTask, LogEnum.pauseHistoryTask,
					userId, LogEnum.historyTask.getValue()+"哈希值："+taskHash);
		}
//		TaskThread task = RunningTask.TASKMAP.getTask(taskHash);
//		if(task==null){
//			result.put("message", "出错了，请联系管理员");
//			result.put("result", false);
//			response.getWriter().write(result.toString());
//		}
//		else{
//			if(task.isAlive()){
//				task.pauseTask();
//				result.put("message", "暂停成功");
//				result.put("result", true);
//				response.getWriter().write(result.toString());
//			}else{
//				result.put("message", "任务已完成");
//				result.put("result", false);
//				response.getWriter().write(result.toString());
//			}
//		}
	}

}
